<template>
  <el-dialog
    :title="signreadonly ? '请假审批' : '重新申请请假'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="请假天数" prop="days">
        <el-input-number v-model="dataForm.days" placeholder="天数" :min="1" v-bind:disabled="signreadonly"></el-input-number>
      </el-form-item>
      <el-form-item label="开始日期" prop="startdate">
        <el-date-picker v-model="dataForm.startdate" type="date" value-format="yyyy-MM-dd" placeholder="开始日期" clearable v-bind:disabled="signreadonly"></el-date-picker>
      </el-form-item>
      <el-form-item label="补充说明" prop="remark">
        <el-input type="textarea" :rows="3" v-model="dataForm.remark" placeholder="补充说明" v-bind:disabled="signreadonly"></el-input>
      </el-form-item>
      <el-form-item v-if="signreadonly" label="审批" size="mini" prop="approve">
        <el-radio-group v-model="dataForm.approve">
          <el-radio :label="1">同意</el-radio>
          <el-radio :label="0">不同意</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        signreadonly: false,
        dataForm: {
          days: '',
          startdate: '',
          remark: '',
          procinstanceid: '',
          leaveid: '',
          approve: 1,
          taskid: ''
        },
        dataRule: {
          days: [
            { required: true, message: '天数不能为空', trigger: 'blur' }
          ],
          startdate: [
            { required: true, message: '开始日期不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '补充说明不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id, updated, taskid) {
        console.log(taskid)
        this.dataForm.taskid = taskid
        this.dataForm.leaveid = id
        if (updated === true) {
          this.signreadonly = false
        } else {
          this.signreadonly = true
        }
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          this.$http({
            url: this.$http.adornUrl(`/flow/leaverequest/info/${this.dataForm.leaveid}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.days = data.data.days
              this.dataForm.startdate = data.data.startdate
              this.dataForm.remark = data.data.remark
              this.dataForm.procinstanceid = data.data.procinstanceid
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.signreadonly) { // 审批
              this.$http({
                url: this.$http.adornUrl('/flow/process/approve'),
                method: 'post',
                data: this.$http.adornData({
                  'taskId': this.dataForm.taskid,
                  'variables': {'approve': this.dataForm.approve}
                })
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false
                      this.$emit('refreshDataList')
                    }
                  })
                } else {
                  this.$message.error(data.msg)
                }
              })
            } else { // 重新申请
              this.$http({
                url: this.$http.adornUrl('/flow/leaverequest/update'),
                method: 'post',
                data: this.$http.adornData({
                  'id': this.dataForm.leaveid,
                  'days': this.dataForm.days,
                  'startdate': this.dataForm.startdate,
                  'remark': this.dataForm.remark
                })
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$http({
                    url: this.$http.adornUrl('/flow/process/approve'),
                    method: 'post',
                    data: this.$http.adornData({
                      'taskId': this.dataForm.taskid,
                      'variables': {'days': this.dataForm.days}
                    })
                  }).then(({data}) => {
                    if (data && data.code === 0) {
                      this.$message({
                        message: '操作成功',
                        type: 'success',
                        duration: 1500,
                        onClose: () => {
                          this.visible = false
                          this.$emit('refreshDataList')
                        }
                      })
                    } else {
                      this.$message.error(data.msg)
                    }
                  })
                } else {
                  this.$message.error(data.msg)
                }
              })
            }
          }
        })
      }
    }
  }
</script>
